package com.iseastar.dianxiaosan.stamp;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iseastar.dianxiaosan.model.StampHistoryBean;
import com.iseastar.dianxiaosan.model.StampHistoryItemBean;
import com.shouhuobao.station.R;

import java.util.ArrayList;

import droid.frame.activity.base.BaseAdapterExt;

/**
 * 邮票商城
 *
 * @author kaka<br>
 *         2017年4月20日
 */
public class StampHistoryAdapter extends BaseAdapterExt<StampHistoryBean> {
    /**
     * 当前正在使用的ID
     */
    private int selectId;

    private boolean showFooter = false;

    private StampBuyHistoryActivity activity;

    public StampHistoryAdapter(ArrayList<StampHistoryBean> items, StampBuyHistoryActivity context) {
        super(items, context);
        //
        if (context instanceof StampBuyHistoryActivity) {
            setEmptyView(R.layout.stamp_buy_history_empty);
        }
        activity = context;
    }

    public int getSelectId() {
        return selectId;
    }

    public void setSelectId(int selectId) {
        this.selectId = selectId;
    }

    /**
     * 是否显示底部Footer
     *
     * @param showFooter
     */
    public void setShowFooter(boolean showFooter) {
        this.showFooter = showFooter;
    }

    @Override
    public int getCount() {
        return super.getCount() + (showFooter ? 1 : 0);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (hasEmptyView()) {
            View layout = super.getView(position, convertView, parent);
            return layout;
        }
        ViewHolder holder = null;
        if (convertView == null || convertView.getTag() == null) {
            convertView = LayoutInflater.from(activity).inflate(R.layout.stamp_buy_history_item, parent, false);
            holder = new ViewHolder();
            holder.orderId = (TextView) convertView.findViewById(R.id.stamp_order_id);
            holder.orderCount = (TextView) convertView.findViewById(R.id.order_price);
            holder.orderTime = (TextView) convertView.findViewById(R.id.order_time);
            holder.stampParent = (LinearLayout) convertView.findViewById(R.id.stamp_price_parent);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        StampHistoryBean history = items.get(position);
        ArrayList<StampHistoryItemBean> items = history.getStamps();
        holder.orderId.setText("订单ID " + history.getOrderId());
        holder.orderCount.setText("共" + items.size() + "件商品, 实付￥" + history.getPayPrice());
        holder.orderTime.setText(history.getPayTime());
        holder.stampParent.removeAllViews();
        for (int i = 0; i < items.size(); i++) {
            StampHistoryItemBean shopBean = items.get(i);
            View view = context.getLayoutInflater().inflate(R.layout.stamp_buy_history_price_item, null);
            TextView stampName = (TextView) view.findViewById(R.id.stamp_name);
            TextView stampCount = (TextView) view.findViewById(R.id.stamp_count);
            TextView stampPrice = (TextView) view.findViewById(R.id.stamp_price);
            stampName.setText(shopBean.getName() + "-" + shopBean.getSeries());
            stampCount.setText(shopBean.getStampNum() + "张");
            stampPrice.setText("￥" + shopBean.getStampPrice());
            holder.stampParent.addView(view);
        }
        return convertView;
    }

    private class ViewHolder {
        private TextView orderId; // 订单id
        private TextView orderTime; // 下单时间
        private TextView orderCount;// 邮票数量和支付金额
        private LinearLayout stampParent;// 系列
    }

}
